package com.xiaoma.h5testdemo.business.main.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import com.xiaoma.common.extend.getDrawableRes
import com.xiaoma.h5testdemo.R
import com.xiaoma.h5testdemo.business.main.utils.XmFragmentStackManager
import com.xiaoma.h5testdemo.business.main.vm.HostActVm
import com.xiaoma.h5testdemo.business.web.ui.ChromeWebFragment
import com.xiaoma.h5testdemo.common.base.BaseSupportFrgBackActivity
import com.xiaoma.h5testdemo.common.constants.Constants
import com.xiaoma.h5testdemo.common.helper.SystemBarHelper
import com.xiaoma.h5testdemo.common.models.PageEntity
import com.xiaoma.h5testdemo.databinding.ActivityMainBinding
import com.xiaoma.lib.log.KLog

/**
 * author: liangjingbo
 * date: 2024/4/18
 * describe:
 */
class HostActivity :
    BaseSupportFrgBackActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {
    private val mHostActVm by viewModels<HostActVm>()
    private var mIsSysNightMode = false
    override fun initView(savedInstanceState: Bundle?) {
        mHostActVm.mXmFragmentStackManager =
            mHostActVm.mXmFragmentStackManager
                ?: XmFragmentStackManager(MainFragment::class.java, this.javaClass.simpleName)
        SystemBarHelper.instance.mIsFullScreen = false
        window.setBackgroundDrawable(getDrawableRes(R.drawable.drawable_window_bg))
        loadMainFragment()
    }


    override fun initEvent(savedInstanceState: Bundle?) {
        super.initEvent(savedInstanceState)
        mHostActVm.actionJumpWeb.observe(this) { vm ->
            vm.onData {
                handleJumpWeb(it)
            }
        }
        mHostActVm.actionFullScreen.observe(this) { vm ->
            vm.onData {
                SystemBarHelper.instance.mIsFullScreen = it
                SystemBarHelper.instance.adapterSystemBar(window, this)
            }
        }
        mHostActVm.actionHiddenNavigationBar.observe(this) { vm ->
            vm.onData {
                if (SystemBarHelper.instance.mOnlyHiddenNavigation != it) {
                    SystemBarHelper.instance.mOnlyHiddenNavigation = it
                    SystemBarHelper.instance.adapterSystemBar(window, this)
                }
            }
        }
        mHostActVm.actionGoToHome.observe(this) { vm ->
            vm.onEvent {
                goToHome()

            }
        }
    }

    private fun goToHome() {
        val removeFrgList = mutableListOf<Fragment>()
        var showFrg: Fragment? = null
        for (fragment in supportFragmentManager.fragments) {
            if (fragment.javaClass == MainFragment::class.java) {
                showFrg = fragment
            } else {
                removeFrgList.add(fragment)
            }
        }
        supportFragmentManager.beginTransaction().apply {
            removeFrgList.forEach {
                remove(it)
            }
            showFrg?.let {
                show(showFrg)
                setMaxLifecycle(it, Lifecycle.State.RESUMED)
            }
            commitAllowingStateLoss()
        }
    }

    private fun handleJumpWeb(url: String) {
        try {
            mHostActVm.mXmFragmentStackManager?.changeFragment(
                supportFragmentManager,
                ChromeWebFragment.newWebFragment(
                    PageEntity(
                        url,
                        Constants.COMMON_USER_AGENT,
                        1,
                        1,
                        false
                    )
                ),
                R.id.fl_main_container
            )
        } catch (e: Exception) {
            e.printStackTrace()
            KLog.e(TAG, "handleJumpWeb() XM_Exception: $e")
        }
    }


    private fun loadMainFragment() {
        mHostActVm.mXmFragmentStackManager?.changeFragment(
            supportFragmentManager,
            MainFragment(),
            R.id.fl_main_container
        )
    }

    override fun onBackEventCallback() {
        var containWeb = false
        for (fragment in supportFragmentManager.fragments) {
            if (fragment.javaClass == ChromeWebFragment::class.java) {
                containWeb = true
            }
        }
        if (containWeb) {
            mHostActVm.mXmFragmentStackManager?.popFragment(supportFragmentManager,R.id.fl_main_container)
        } else {
            finish()
        }
    }

}